package com.jadcon.bedbreakfast.domain;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class ReservationValidator implements Validator {

	    
	    /**
	    * This Validator validates just Reservation instances
	    */
	    public boolean supports(Class clazz) {
	        return Reservation.class.equals(clazz);
	    }
	    
	    /**
	     * Does the actual validation.  Validation errors
	     * are set on the Errors class that Spring provides.
	     * Later the errors will be displayed to the user.
	     */
	    public void validate(Object object, Errors e) {
	        Reservation reservation = (Reservation) object;
	        String guestName = reservation.getGuestName();
	        String address = reservation.getAddress1();
	        String city = reservation.getCity();
	        int zip = reservation.getZip();
	        String state = reservation.getState();
	        String phone = reservation.getPhoneNumber();
	        String email = reservation.getEmail();
	        if (guestName == null || "".equals(guestName)) {
	        	// if the name is null or empty,
	        	// create an error message.
	        	// The first parameter is the name
	        	// of the field (which is this case is "guestName")
	        	// and the 2nd parameter is the message key
	        	// for the error message.  The key corresponds
	        	// to a message in /src/main/resources/messages.properties
	        	e.rejectValue("guestName", "guestName.empty");
	        }
	        else if (address == null || "".equals(address)){
	        	e.rejectValue("address", "address.empty");
	        }
	        else if (city == null || "".equals(city)){
	        	e.rejectValue("city", "city.empty");
	        }
	        else if (state ==null || "".equals(state)){
	        	e.rejectValue("state", "state.empty");
	        }
	        else if (!(zip>=00000 && zip<=99999)){
	        	e.rejectValue("zip", "zip.invalid");	
	        }
	        else if (phone == null || "".equals(phone)){
	        	e.rejectValue("phone", "phone.empty");
	        }
	        else if (email == null || "".equals(email)){
	        	e.rejectValue("email", "email.empty");
	        }
	        
	        // TODO - validate check in and check out
	        
	        
	    }
}
